package com.yxinsur.imc.web.controller.business;

import com.yxinsur.imc.business.domain.ImcComm;
import com.yxinsur.imc.business.domain.ImcCommRisk;
import com.yxinsur.imc.business.form.CommRiskForm;
import com.yxinsur.imc.business.mapper.ImcCommMapper;
import com.yxinsur.imc.business.service.IImcCommRiskService;
import com.yxinsur.imc.business.vo.CommRiskVo;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.core.page.TableDataInfo;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 佣金计算险种明细Controller
 *
 * @author imc
 * @date 2021-05-26
 */
@Api(value = "佣金计算险种明细接口服务",tags = {"佣金计算险种明细接口服务"})
@RestController
@RequestMapping("/business/commRisk")
public class ImcCommRiskController extends BaseController {
    @Autowired
    private IImcCommRiskService imcCommRiskService;

    /**
     * 查询佣金计算险种明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ImcCommRisk imcCommRisk) {
        startPage();
        List<ImcCommRisk> list = imcCommRiskService.selectImcCommRiskList(imcCommRisk);
        return getDataTable(list);
    }

    /**
     * 查询险种明细
     * @param commRiskForm
     * @return
     */
    @ApiOperation("查询险种明细")
    @PostMapping("/commList")
    public TableDataInfo list(CommRiskForm commRiskForm) {
        startPage();
        List<CommRiskVo> list = imcCommRiskService.selectCommRiskVoList(commRiskForm);
        return getDataTable(list);
    }

    /**
     * 新增保单查询
     * @param policyCode
     * @return
     */
    @ApiOperation("新增保单查询")
    @PostMapping("/commOrder/{policyCode}/{commId}")
    public TableDataInfo commOrder(@PathVariable("policyCode") String policyCode,@PathVariable("commId")Long commId) {
        startPage();
        List<CommRiskVo> list = imcCommRiskService.commOrder(policyCode,commId);
        return getDataTable(list);
    }

    /**
     * 添加保单到险种明细
     */
    @ApiOperation("添加保单到险种明细")
    @Log(title = "佣金计算险种明细", businessType = BusinessType.INSERT)
    @PostMapping("/addToCommRisk/{commId}")
    public AjaxResult addToCommRisk(@PathVariable("commId") Long commId,@RequestBody List<Long> orderRiskIds) {
        try {
            //添加保单前做 佣金主表状态校验
            if(!imcCommRiskService.checkCommStatus(commId)){
                return AjaxResult.error("佣金状态不为待计算，不能添加保单");
            }
            imcCommRiskService.addToCommRisk(commId,orderRiskIds);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }




    /**
     * 导出佣金计算险种明细列表
     */
    @ApiOperation("佣金-险种明细导出")
    @Log(title = "佣金计算险种明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ImcCommRisk imcCommRisk) {
        List<ImcCommRisk> list = imcCommRiskService.selectImcCommRiskList(imcCommRisk);
        ExcelUtil<ImcCommRisk> util = new ExcelUtil<ImcCommRisk>(ImcCommRisk.class);
        return util.exportExcel(list, "risk");
    }

    /**
     * 修改佣金计算险种明细
     */
    @Log(title = "佣金计算险种明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImcCommRisk imcCommRisk) {
        return toAjax(imcCommRiskService.updateImcCommRisk(imcCommRisk));
    }

    /**
     * 删除佣金计算险种明细
     */
    @ApiOperation("删除佣金计算险种明细")
    @Log(title = "佣金计算险种明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        ImcCommRisk imcCommRisk = imcCommRiskService.selectImcCommRiskById(ids[0]);
        try {
            if(!imcCommRiskService.checkCommStatus(imcCommRisk.getCommId())){
                return AjaxResult.error("佣金状态不为待计算，不能删除");
            }
            imcCommRiskService.deleteImcCommRiskByIds(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
